//
//  NSArray+wgExpand.m
//  WGTool
//
//  Created by czw on 2022/8/19.
//

#import "NSArray+wgExpand.h"

@implementation NSArray (wgExpand)

- (id)wg_objAtIndex:(NSInteger)index{
   
    if (index >=0) {
        return [self objectAtIndex:index];
    }else{
        return [self objectAtIndex:self.count +index];
    }
}

- (BOOL)wg_isEmpty{
    return self.count<=0;
}

- (NSArray*)wg_mapper:(id(^)(id obj))mapper{
    NSMutableArray *array = [NSMutableArray array];
    for (id item in self) {
        if (mapper) {
            id newItem =  mapper(item);
            [array addObject:newItem];
        }
    }
    return array.copy;
}

- (NSArray*)wg_filter:(BOOL(^)(id obj))filter{
    NSMutableArray *array = [NSMutableArray array];
    for (id item in self) {
        if (filter) {
            BOOL isFit =  filter(item);
            if (isFit) {
                [array addObject:item];
            }
        }
    }
    return array.copy;
}


-(NSString *(^)(NSString *separator))wg_joinedByString{
    return  ^(NSString *separator) {
        return  [self componentsJoinedByString:separator];
    };
}
@end
